package com.pdformparent;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.dengpp.pdform.base.BaseCustomFormView;

import java.util.ArrayList;
import java.util.List;
// CheckboxGroupView.java

/**
 * 多选框自定义视图
 */
public class CheckboxGroupView extends BaseCustomFormView {
    private LinearLayout container;
    private List<String> selectedValues = new ArrayList<>();
    private List<String> options;

    @Override
    protected View onCreateView(Context context) {
        container = new LinearLayout(context);
        //垂直布局
        container.setOrientation(LinearLayout.VERTICAL);

        // 设置布局参数，确保视图可见
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        container.setLayoutParams(layoutParams);

        return container;
    }

    @Override
    protected void onBindData(Object value) {
        // 从配置中获取选项
        if (attribute != null && attribute.getOptions() != null) {
            options = attribute.getOptions();
        }

        // 恢复选中值
        if (value instanceof List) {
            selectedValues = new ArrayList<>((List<String>) value);
        } else if (value == null) {
            selectedValues = new ArrayList<>();
        }

        // 创建复选框
        createCheckboxes();
    }

    @Override
    public Object getCurrentValue() {
        return selectedValues;
    }

    private void createCheckboxes() {
        // 清除之前的选项
        container.removeAllViews();

        if (options == null || options.isEmpty()) {
            // 如果没有选项，显示提示信息
            CheckBox emptyCheckbox = new CheckBox(context);
            emptyCheckbox.setText("暂无选项");
            emptyCheckbox.setEnabled(false);
            container.addView(emptyCheckbox);
            return;
        }

        for (String option : options) {
            CheckBox checkBox = new CheckBox(context);
            checkBox.setText(option);
            checkBox.setTag(option);

            // 设置选中状态
            checkBox.setChecked(selectedValues.contains(option));

            // 设置点击监听器
            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                String value = (String) buttonView.getTag();
                if (isChecked) {
                    if (!selectedValues.contains(value)) {
                        selectedValues.add(value);
                    }
                } else {
                    selectedValues.remove(value);
                }
                notifyValueChanged(selectedValues);
            });

            // 设置布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(0, 8, 0, 8); // 添加间距
            checkBox.setLayoutParams(params);

            container.addView(checkBox);
        }
    }
}
